
package elementos;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;



public class Bar implements Drawable{
private int x,y,width,height;
    private Color color;
    private int number;
    private int vida;
    private ArrayList<KillBrickListener> l2;
    private double speedInc=1.5;
    
    public Bar(int x, int y, int width, int height,Color color, int number,int vida){
        this.x=x;
        this.y=y;
        this.width=width;
        this.height=height;
        this.color=color;
        this.number=number;
        this.vida=vida;
        l2= new ArrayList<KillBrickListener>();
    }
    
    public void perderVida(){
        vida--;
    }
    
    public void addKillBricklListener(KillBrickListener o){
        l2.add(o);
    }
    public void clearKillBrickListeners(){
        l2.clear();
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }
        
    public int getNumber() {
        return number;
    }
    
    public void moveUp(){
        y-=10;
    }
    
    public void moveDown(){
        y+=10;
    }

    @Override
    public void render(Graphics g) {
        Color c = g.getColor();
        g.setColor(color);
        g.fillRect(x, y, width, height);
        g.setColor(c);
    }

    @Override
    public void add(Drawable d) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void remove(Drawable d) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Drawable getChild(int i) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Drawable collision(Drawable d) {
        if(d instanceof Ball){
            Ball b = (Ball) d;
            //DETECCION COLISION SUPERIOR IZQUIERDO
            if (b.getY() <= y + height) {
                if (b.getY() >= y) {
                    if (b.getX() >= x) {
                        if (b.getX() <= x + width) {
                            //QUE BORDE EN CONCRETO ESTA SITUADO E INVERTIMOS EL DESPLAZAMIENTO PARA CADA CASO
                            if (b.getY() == y + height) {
                                b.invertY();
                                b.setDx((float)(b.getDX() + speedInc));
                                b.setDy((float)(b.getDY() + speedInc));
                                return b;
                            }
                            if (b.getY() == y) {
                                b.invertY();
                                b.setDx((float) (b.getDX() * speedInc));
                                b.setDy((float) (b.getDY() * speedInc));
                                return b;
                            }
                            if (b.getX() == x) {
                                b.invertX();
                                b.setDx((float) (b.getDX() * speedInc));
                                b.setDy((float) (b.getDY() * speedInc));
                                return b;
                            }
                            if (b.getX() == x + width) {
                                b.invertX();
                                b.setDx((float) (b.getDX() * speedInc));
                                b.setDy((float) (b.getDY() * speedInc));
                                return b;
                            }
                        }
                    }
                }
            }
            return null; // No colisionó
        }else if (d instanceof Brick){
            Brick b = (Brick) d;
            //miramos la altura
            //comprobamos que esta en el rango de las alturas el brick contando que el brick puede sobresalir de la bar
            if(b.getY()>this.y -b.getHeight() && (b.getY()+b.getHeight()<(y+height + b.getHeight()))){
                //2 posibles (derecha o izq)
                if(b.getX()<(x+width) && b.getX()+b.getWidth()>this.x+width){
                    //colision
                    Kill(b);
                    return b;
                }
                if((b.getX()+b.getWidth())>x&& b.getX()<x){
                    //colision
                    Kill(b);
                    return b;
                }
            }
        }
        return null;
    }
    
    public boolean BrickCollision(Drawable d){
        if (d instanceof Brick) {
            Brick b = (Brick) d;
            //miramos la altura
            //comprobamos que esta en el rango de las alturas el brick contando que el brick puede sobresalir de la bar
            if (b.getY() > this.y - b.getHeight() && (b.getY() + b.getHeight() < (y + height + b.getHeight()))) {
                //2 posibles (derecha o izq)
                if (b.getX() < (x + width) && b.getX() + b.getWidth() > this.x + width) {
                    //colision
                    return true;
                    
                }
                if ((b.getX() + b.getWidth()) > x && b.getX() < x) {
                    //colision
                    return true;
                   
                }
            }
        }
        return false;
    }
    
    public void Kill(Brick b){
        for(int i=0;i<l2.size();i++){
            l2.get(i).onKill(b);
        }
    }
   
}
